/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.beans.PropertyChangeEvent;
import Model.Reservation;
import Controller.SejourController;
import Model.Categorie;
import java.awt.BorderLayout;
import java.text.ParseException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
/**
 *
 * @author Hugo
 */
public class InfoReservation extends AbstractViewPanel {
    private Reservation _reservation;
    private SejourController _controller;
    private int _idSejour;
    /**
     * Creates new form InfoReservation
     */
    public InfoReservation(Reservation r, SejourController controller, int idSejour) {
        initComponents();
        _reservation = r;
        _controller = controller;
        _idSejour = idSejour;
        this.lblNomClient.setText(_reservation.getClient().getNomClient());
        this.lblNumConf.setText(Integer.toString(_reservation.getNumConfirmation()));
        this.lblNumTel.setText(_reservation.getClient().getNumTel());
        
        populerTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblNomClient = new javax.swing.JLabel();
        lblNumConf = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblNumTel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblLigneReservation = new javax.swing.JTable();
        btnOk = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Numéro de Confirmation:");

        jLabel2.setText("Nom Client:");

        lblNomClient.setText("jLabel1");

        lblNumConf.setText("jLabel1");

        jLabel7.setText("Numéro de téléphone:");

        lblNumTel.setText("jLabel1");

        tblLigneReservation.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblLigneReservation);

        btnOk.setText("OK");
        btnOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOkMouseClicked(evt);
            }
        });
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(270, 270, 270)
                        .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(196, 196, 196)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNumConf)
                            .addComponent(lblNomClient)
                            .addComponent(lblNumTel))))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblNumConf))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNomClient)
                    .addComponent(jLabel2))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lblNumTel))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnOk)
                .addContainerGap(27, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOkMouseClicked
        int[] row_indexes= tblLigneReservation.getSelectedRows();
        String dteDebut = "";
        String dteFin = "";
        String nomCategory = "";
        String nbrChambre = "";
        for(int i=0;i<row_indexes.length;i++){
            nbrChambre = tblLigneReservation.getValueAt(row_indexes[i], 0).toString(); 
            nomCategory = tblLigneReservation.getValueAt(row_indexes[i], 1).toString(); 
            dteDebut = tblLigneReservation.getValueAt(row_indexes[i], 2).toString(); 
            dteFin = tblLigneReservation.getValueAt(row_indexes[i], 3).toString(); 
        }
        
        if(dteDebut.equals("") || dteFin.equals("") || nomCategory.equals("")){
            JOptionPane.showMessageDialog(this, "Veuillez sélectionner une rangée");
        }
        else{
            
            Date dateFin;
            Date dateDebut;
            
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy"); 
            dateFormat.setLenient(false);
            
            dateDebut = getDate(this.tblLigneReservation.getValueAt(this.tblLigneReservation.getSelectedRow(),2).toString());
            dateFin = getDate(this.tblLigneReservation.getValueAt(this.tblLigneReservation.getSelectedRow(),3).toString());
            
            this._controller.setDteDebut(dateDebut);
            this._controller.setDteFin(dateFin);
            
            FournirChambre fournirChambreVue = new FournirChambre(_controller, dateDebut , dateFin, Integer.parseInt(nbrChambre),nomCategory,_idSejour);
            this._controller.addView(fournirChambreVue);
            JFrame displayFrame = new JFrame("Fournir les chambres au clients");
            displayFrame.getContentPane().add(fournirChambreVue, BorderLayout.CENTER);
            displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            displayFrame.pack();
            displayFrame.setVisible(true);
            this.getTopLevelAncestor().setVisible(false);
        }
        
    }//GEN-LAST:event_btnOkMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblNomClient;
    private javax.swing.JLabel lblNumConf;
    private javax.swing.JLabel lblNumTel;
    private javax.swing.JTable tblLigneReservation;
    // End of variables declaration//GEN-END:variables

    private void populerTable(){
        String[][] ligneReservation =CréerTableau2D();
         
        String[] celNames;
        
        celNames = new String[4];
        celNames[0] = "Nombre de chambre";
        celNames[1] = "Nom de la catégorie";
        celNames[2] = "Date d'arrivée";
        celNames[3] = "Date de départ";
        
        JTable jtbl = new JTable(ligneReservation, celNames);
        this.tblLigneReservation.setModel(new DefaultTableModel());
        this.tblLigneReservation.setModel(jtbl.getModel());        
    }
    
    private String[][] CréerTableau2D(){
        String[][] data = new String[_reservation.getligneReservation().size()][4];
        
        for(int i=0; i < _reservation.getligneReservation().size(); i++)
        {
            data[i][0] = String.valueOf(_reservation.getligneReservation().get(i).getNombreChambre());
            data[i][1] = _reservation.getligneReservation().get(i).getCat().getNomCategorie();
            data[i][2] = DateToString(_reservation.getligneReservation().get(i).getDteArrive());
            data[i][3] = DateToString(_reservation.getligneReservation().get(i).getDteFin());
        }
        
        return data;
    }
    
    private String DateToString(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        
        StringBuilder stringDate = new StringBuilder(dateFormat.format(date));
        
        return stringDate.toString();
    }
    
    private Date getDate(String date){
        Date now = Calendar.getInstance().getTime();
        Date dteFormatte = new Date();
        boolean isRealDate = true;
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
        dateFormat.setLenient(false);
        
        try{
            dteFormatte = dateFormat.parse(date);
        }catch (ParseException e){
            JOptionPane.showMessageDialog(this, "Veuillez entré une date du format dd/MM/yyyy.", "La date entré n'est pas bonne", 0);
            isRealDate = false;
        }
        if(isRealDate){
            if(dteFormatte.before(now)){
                isRealDate = false;
                JOptionPane.showMessageDialog(this, "Veuillez entré une date après aujourd'hui", "La date entré n'est pas bonne", 0);
            }
        }
        if(isRealDate){
            return dteFormatte;
        }else
            return null;
    }
    
    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        
    }
}
